using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Threading;
using System.Xml;
using System.Xml.Serialization;
using System.Security.Cryptography;
using VodafoneWP7.Resources;


namespace VodafoneWP7
{
    public class WebConnection
    {

        public static readonly string VERSION = "1.0.0.0";

        public static void updateOtherProduct(Action onSuccess)
        {
            try
            {
                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://chainingexpwp7.altervista.org/appList_"+Strings.LangId+".php");
               
                req.Method = "GET";
                req.BeginGetResponse(
                    (asyncResult) =>
                    {
                        try
                        {                            
                            HttpWebResponse objResponse = null;
                            StreamReader objs = null;

                            HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;

                            objResponse = (HttpWebResponse)request.EndGetResponse(asyncResult);
                            //objs = new StreamReader(objResponse.GetResponseStream());

                            var stream = objResponse.GetResponseStream();
                            byte [] buffer= new byte[stream.Length];
                            stream.Read(buffer,0,(int)stream.Length);
                            var enc = Encoding.GetEncoding("ISO-8859-1");
                            try
                            {
                                var ct = objResponse.Headers["Content-Type"];
                                var charset = ct.Split(',').First(st => st.Contains("charset")).Split('=')[1];
                                enc = Encoding.GetEncoding(charset);
                            }
                            catch (Exception) { }
                            var html = enc.GetString(buffer, 0, (int)stream.Length);
                            //var html = objs.ReadToEnd();
                            var apps_list = html.Split('\n').Select(app => app.Split('|').ToArray()).ToList();
                            //var apps_list_ids = apps_list.Select(app => app[0]);

                            //var to_remove = StateAndOptions.state.applications2.Keys.Except(apps_list_ids);
                            
                            ////foreach (var i in to_remove) 
                            ////    StateAndOptions.state.applications2.Remove(i);
                            var newAppList = new List<OurApplication>();

                            foreach (var a in apps_list)
                            {
                                //var a = apps_list.FirstOrDefault(app => app[0].Equals(id));
                                //if (StateAndOptions.state.applications2.Count(appl=>appl.id.Equals(id))==0 || StateAndOptions.state.applications2.First(appl=>appl.id.Equals(id)).revision < Int32.Parse(a[1]))
                                //{
                                    //var old = StateAndOptions.state.applications2.FirstOrDefault(appl => appl.id.Equals(id));
                                    //if (old != null) StateAndOptions.state.applications2.Remove(old);
                                    try
                                    {
                                        newAppList.Add(
                                            new OurApplication()
                                            {
                                                id=a[0],
                                                name = a[1],
                                                imageUri = a[2],                                             
                                                description=a[3]
                                            }
                                            );
                                    }
                                    catch (Exception)
                                    {
                                    }
                                    
                               }
                            ApplicationState.state.applications = newAppList;
                                                        
                            try
                            {
                                onSuccess();
                            }
                            catch (Exception) { }

                        }
                        catch (Exception e)
                        {
                            var prova = e;
                            e = e;
                        }
                    }, req);
            }
            catch (Exception e)
            {
            }
        }

        public static void GetRequestStreamCallbackForBook(IAsyncResult asyncResult)
        {
            IAsyncResult ar = null;

            int i = 0;
        }


        protected static void GetScrapingResponse(IAsyncResult result)
        {
            int i = 1;
        }



    }

}
